<?php
declare (strict_types=1);

namespace app\admin\controller;

use addons\Builder\facade\Builder;
use app\common\model\Assets;
use think\App;

class Account extends Base
{
    //主键，默认为id
    protected string $pk = 'id';

    //主模型
    protected string $model = '\app\common\model\Account';

    //关联模型
    protected array $hasOne = ['user', 'take'];

    //数据排序，支持多个
    protected array $sort = ['id' => 'desc'];

    //账户流水
    public function index(): string
    {
        return $this->fetch('index');
    }

    //奖金明细
    public function bonus(): string
    {
        return $this->fetch('bonus', ['is_bonus', '=', 1]);
    }

    //充值明细
    public function recharge(): string
    {
        return $this->fetch('recharge', ['is_admin', '=', 1]);
    }

    public function fetch($action, $extra_map = []): string
    {
        $table = Builder::Table()
            ->addColumn(['field' => 'id', 'title' => 'ID', 'sort' => true, 'width' => 80])
            ->addColumn(['field' => 'uid', 'title' => '相关会员', 'templet' => '#_userTpl', 'width' => 240])
            ->addColumn(['field' => 'type', 'title' => '账户']);
        $search = [
            ['uid', '会员编号/昵称/手机号', 'input,Like', '', '\app\common\model\User', 'code|nickname|mobile,id'],
            ['type', '账户类型', 'select', Assets::getFields(3)],
            ['source', '来源会员编号/昵称/手机号', 'input,Like', '', '\app\common\model\User', 'code|nickname|mobile,id'],
            ['category', '奖金类型', 'select', getBonusTitle(1)]
        ];
        switch ($action) {
            case "index":
                $table->addColumn(['field' => 'front', 'title' => '前余额'])
                    ->addColumn(['field' => 'amount', 'title' => '金额', 'templet' => '<div><span class="color color_{{d.fx}}">{{d.fx}}{{d.amount}}</span></div>'])
                    ->addColumn(['field' => 'after', 'title' => '后余额'])
                    ->addColumn(['title' => '来源会员', 'templet' => '#_takeUserTpl']);
                unset($search[3]);
                break;
            case "bonus":
                $table->addColumn(['field' => 'amount', 'title' => '金额', 'templet' => '<div><span class="color color_{{d.fx}}">{{d.fx}}{{d.amount}}</span></div>'])
                    ->addColumn(['title' => '来源会员', 'templet' => '#_takeUserTpl']);
                break;
            case "recharge":
                $table->addColumn(['field' => 'amount', 'title' => '金额', 'templet' => '<div><span class="color color_{{d.fx}}">{{d.fx}}{{d.amount}}</span></div>'])
                    ->addColumn(['field' => 'msg', 'title' => '备注'])
                    ->addColumn(['field' => 'operator', 'title' => '操作人'])
                    ->addTopButtons([
                        'recharge' => [
                            'title' => '充值', 'icon' => 'layui-icon-util',
                            'url'   => url('user/recharge')->build(),
                            'event' => '_openDrawer',
                            'size'  => '50%'
                        ],
                    ]);
                unset($search[2], $search[3]);
                break;
        }

        return $table->addColumn(['field' => 'intro', 'title' => '说明'])
            ->addColumn(['field' => 'create_time', 'title' => '时间'])
            ->addExtraMap($extra_map ? [$extra_map] : [])
            ->addTopButtons(['export'])
            ->addSearch($search)
            ->fetch();
    }
}